(function($){
	var inHosRecordConfig=[
		{
			field:'patientGender',
			fieldName:'性别'
		},{
			field:'patientBirthday',
			fieldName:'出生日期'
		},{
			field:'patientFolk',
			fieldName:'民族'
		},{
			field:'nativePlace',
			fieldName:'籍贯'
		},{
			field:'height',
			fieldName:'身高'
		},{
			field:'weight',
			fieldName:'体重'
		},{
			field:'bloodType',
			fieldName:'血型'
		},{
			field:'maritalStatus',
			fieldName:'婚姻状况'
		},{
			field:'job',
			fieldName:'工作'
		},{
			field:'healthcareType',
			fieldName:'医保类型'
		},{
			field:'hctId',
			fieldName:'医保卡号'
		},{
			field:'healthCardId',
			fieldName:'健康卡号'
		},{
			field:'patientIdNumber',
			fieldName:'身份证号'
		},{
			field:'patientPhoneNo',
			fieldName:'联系方式'
		},{
			field:'contactName',
			fieldName:'联系人'
		},{
			field:'contactPhoneNo',
			fieldName:'联系人电话'
		},{
			field:'patientAddress',
			fieldName:'现住址'
		},{
			field:'clinicHistory',
			fieldName:'病史'
		},{
			field:'infectiousHistory',
			fieldName:'传染病史'
		},{
			field:'operationHistory',
			fieldName:'手术史'
		},{
			field:'obstericalHistory',
			fieldName:'婚育史'
		},{
			field:'transfutionHistory',
			fieldName:'输血史'
		},{
			field:'allergyHistory',
			fieldName:'过敏史'
		},{
			field:'preventiveInoculationHistory',
			fieldName:'预防接种史'
		},{
			field:'personalHistory',
			fieldName:'个人史'
		},{
			field:'menstrualHistory',
			fieldName:'月经史'
		},{
			field:'familyHistory',
			fieldName:'家族史'
		},{
			field:'morbidState',
			fieldName:'入院疾病状态'
		},{
			field:'patientStatus',
			fieldName:'患者病情'
		},{
			field:'exdepartmentName',
			fieldName:'原就诊科室'
		},{
			field:'preDiagnose',
			fieldName:'诊断'
		},{
			field:'illness',
			fieldName:'查体'
		},{
			field:'clinicDiagnose',
			fieldName:'临床诊断'
		},{
			field:'pathologicalDiagnosis',
			fieldName:'病理诊断'
		},{
			field:'diagnosisCode',
			fieldName:'诊断编码'
		},{
			field:'applyType',
			fieldName:'申请类型'
		},{
			field:'patientDescribe',
			fieldName:'主述'
		},{
			field:'assistantIllness',
			fieldName:'辅助检查'
		},{
			field:'takenMedicien',
			fieldName:'入院后用药'
		},{
			field:'treatmentProcess',
			fieldName:'治疗经过'
		},{
			field:'reqConsult',
			fieldName:'会诊目的'
		},{
			field:'supplementInstruction',
			fieldName:'补充说明'
		}
	]
	
	$(document).ready(function(){
		//事件绑定
		eventBind();
		if(patientInfo){
			//加载入院记录
			initInHosRecord();
			//加载首次病程记录
			initDocument('首次病程记录','#shoucibingchengTable');
			//加载查房记录
			initDocument('查房记录','#chafangTable');
			//加载病程记录
			initDocument('病程记录','#bingchengTable');
			//加载会诊记录
			initDocument('会诊记录','#huizhenTable');
			//加载护理记录
			initDocument('护理记录','#huliTable');
			//加载出院记录
			initDocument('出院记录','#chuyuanTable');
		}
	});
	
	/**
	 * 事件绑定
	 */
	function eventBind(){
		//tab切换
		$("#bingliTabs li").click(function(){
		    $("#bingliTabs li").removeClass('on');
		    $(this).addClass('on');
		    var tab =  $(this).data('tab');
		    $("div [id^='tab_']").hide();
		    $("#tab_"+tab).show();
		 })
	}
	
	/**
	 * 加载入院记录
	 * @returns
	 */
	function initInHosRecord(){
		var data={};
		$.each(inHosRecordConfig,function(index,item){
			item.fieldVal=patientInfo[item.field];
		})
		data.tableData=inHosRecordConfig;
		var tableHtml = template('tpl-ruyuanTable', data);
		$("#ruyuanTable").html(tableHtml);
	}
	
	/**
	 * 加载文书
	 * @returns
	 */
	function initDocument(documentType,docTableId){
		$.getApiClient('/patientView/documents/{patientId}').setQuery({
			patientId : patientId,
			documentType : documentType
		}).get().done(function(data){
			if(data.code=='0'){
				var documents=data.payload;
				$.each(documents,function(index,item){
					item.submitTime=$.dateFormatToString(new Date(item.submitTime),'yyyy-MM-dd HH:mm');
					if(item.documentFormat=='txt'){
						$.getApiClient('/patientView/document/{documentId}/text',false).setQuery({
							documentId : item.id
						}).get().done(function(resData){
							if(resData.code=='0'){
								item.documentText=resData.payload;
							}
						});
					}
				})
				
				var data={};
				data.tableData=documents;
				var tableHtml = template('tpl-document', data);
				$(docTableId).html(tableHtml);
				//文书打开事件绑定
				openDocEvent(docTableId);
			}
		});
		
		//文书打开事件绑定
		function openDocEvent(docTableId){
			$(docTableId).find('a').click(function(){
				var dialogData={};
				dialogData.documentId = $(this).data('documentid');
				dialogData.documentFormat = $(this).data('documentformat');
				var dialogHtml = template('tpl-document-dialog', dialogData);
				if(dialogData.documentFormat=='jpg'|| dialogData.documentFormat=='png'|| dialogData.documentFormat=='pdf'){
					//弹出层
					layer.open({
					    type: 1,
					    title: '查看文书',
					    maxmin: true,
					    area: ['900px', '650px'],
					    content: dialogHtml,
					    success: function(layero, index){
//					      if(dialogData.documentFormat=='pdf'){
//					    	  //加载PDF文件
//					    	  PDFObject.embed(path+'/api/patientView/document/'+dialogData.documentId+'/stream', "#pdfContainer");
//					      }
					    }
					  });
				}else{
					$.download('/patientView/document/'+dialogData.documentId+'/stream');
				}
			})
		}
	}
})(jQuery);
